{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "import codecs\n",
    "import json\n",
    "import networkx as nx\n",
    "import pandas as pd\n",
    "from matplotlib import pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "f = codecs.open(\"./data/dblp/author2articles/data.json\")\n",
    "content = f.read()\n",
    "content_json = json.loads(content)\n",
    "f.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "year2article2coworkers = {}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "for author in content_json.keys():\n",
    "    for title, year in content_json[author].items():\n",
    "        if  \"big data\" in title:\n",
    "            pass\n",
    "        else:\n",
    "            continue\n",
    "        article2coworkers = year2article2coworkers.get(int(year),{})\n",
    "        coworkers = article2coworkers.get(title, set())\n",
    "        coworkers.add(author)\n",
    "        article2coworkers[title] = coworkers\n",
    "        year2article2coworkers[int(year)] = article2coworkers"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "for year in sorted(list(year2article2coworkers.keys())):\n",
    "    break"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1991, 1994, 1995, 2006, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sorted(list(year2article2coworkers.keys()))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "years = [2015, 2016, 2017, 2018, 2019]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = []"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2015\n",
      "author: Ikki Fujiwara, centrality: 0.10370\n",
      "author: Stephan Olariu, centrality: 0.10370\n",
      "author: Chau Yuen, centrality: 0.10370\n",
      "author: D. Scott Wills, centrality: 0.10370\n",
      "author: Li Lu 0001, centrality: 0.10370\n",
      "author: Haibing Guan, centrality: 0.10370\n",
      "author: Jinquan Dai, centrality: 0.10370\n",
      "author: Chunbai Yang, centrality: 0.10370\n",
      "author: Guangwen Yang, centrality: 0.10370\n",
      "author: Isabelle Puaut, centrality: 0.10370\n",
      "2016\n",
      "author: Naren Ramakrishnan, centrality: 0.09373\n",
      "author: Zied Lachiri, centrality: 0.09366\n",
      "author: Ying-Cherng Lan, centrality: 0.09366\n",
      "author: Roberto Uribeetxeberria, centrality: 0.09366\n",
      "author: Mingwei Li, centrality: 0.09366\n",
      "author: Tom Schipper, centrality: 0.09366\n",
      "author: Cong Li, centrality: 0.09366\n",
      "author: Riad Toufouti, centrality: 0.09366\n",
      "author: Laurent Delosi&egrave;res, centrality: 0.09366\n",
      "author: Salah Saad, centrality: 0.09366\n",
      "2017\n",
      "author: Natalia Juristo Juzgado, centrality: 0.05027\n",
      "author: Na Wang, centrality: 0.05026\n",
      "author: Pietro Hiram Guzzi, centrality: 0.05025\n",
      "author: Bruno Cuconato, centrality: 0.05025\n",
      "author: Shivani Sheth, centrality: 0.05025\n",
      "author: Mayukh Nair, centrality: 0.05025\n",
      "author: Ahmed E. Hassan, centrality: 0.05025\n",
      "author: Sohrab P. Shah, centrality: 0.05025\n",
      "author: Vinayak Agarwal, centrality: 0.05025\n",
      "author: Jagath Gunatilake, centrality: 0.05025\n",
      "2018\n",
      "author: Yu Zhang, centrality: 0.05618\n",
      "author: Jie Li, centrality: 0.05604\n",
      "author: Chao Wang, centrality: 0.05604\n",
      "author: Yang Li, centrality: 0.05604\n",
      "author: Mohamed Othman, centrality: 0.05600\n",
      "author: Zhen Guo, centrality: 0.05600\n",
      "author: Jing Zhang, centrality: 0.05599\n",
      "author: Ting Qu, centrality: 0.05599\n",
      "author: Xiang Li, centrality: 0.05599\n",
      "author: Marjan Kuchaki Rafsanjani, centrality: 0.05599\n",
      "2019\n",
      "author: Ling Zhang, centrality: 0.05459\n",
      "author: Wei Liu, centrality: 0.05457\n",
      "author: Liping Di, centrality: 0.05457\n",
      "author: Chi Zhang, centrality: 0.05457\n",
      "author: Wei Song, centrality: 0.05456\n",
      "author: Cheng Wang, centrality: 0.05456\n",
      "author: Jie Yang, centrality: 0.05456\n",
      "author: Yichen Zhang, centrality: 0.05456\n",
      "author: Peng Gao, centrality: 0.05455\n",
      "author: Robert Musk, centrality: 0.05455\n"
     ]
    }
   ],
   "source": [
    "for year in years:\n",
    "    G = nx.Graph()\n",
    "    for title, authors in year2article2coworkers[year].items():\n",
    "        authors = list(authors)\n",
    "        if len(authors) < 2:\n",
    "            continue\n",
    "        length = len(authors)\n",
    "        for i in range(length):\n",
    "            G.add_node(authors[i])\n",
    "            for j in range(i+1, length):\n",
    "                G.add_edge(authors[i], authors[j])\n",
    "    eigenvector_centrality  = nx.centrality.eigenvector_centrality(G)\n",
    "    print(year)\n",
    "    sortted_centralities = sorted(eigenvector_centrality.items(), key= lambda x: x[1], reverse= True)[:10]\n",
    "    for name, centrality in sortted_centralities:\n",
    "        print(\"author: {}, centrality: {:.5f}\".format(name, centrality))\n",
    "    data.append({})\n",
    "    data[-1][\"year\"] = year\n",
    "    for index, (name, centrality) in enumerate(sortted_centralities):\n",
    "        data[-1][\"top{}_author\".format(index+1)] = name\n",
    "        data[-1][\"top{}_centrality\".format(index+1)] = centrality"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.DataFrame(data)\n",
    "df = df.set_index(\"year\")\n",
    "orders = [(\"top{}_author\".format(index+1), \"top{}_centrality\".format(index+1)) for index in range(10)]\n",
    "flatten_orders = []\n",
    "for order in orders:\n",
    "    flatten_orders.append(order[0])\n",
    "    flatten_orders.append(order[1])\n",
    "df = df.loc[:, flatten_orders]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>top1_author</th>\n",
       "      <th>top1_centrality</th>\n",
       "      <th>top2_author</th>\n",
       "      <th>top2_centrality</th>\n",
       "      <th>top3_author</th>\n",
       "      <th>top3_centrality</th>\n",
       "      <th>top4_author</th>\n",
       "      <th>top4_centrality</th>\n",
       "      <th>top5_author</th>\n",
       "      <th>top5_centrality</th>\n",
       "      <th>top6_author</th>\n",
       "      <th>top6_centrality</th>\n",
       "      <th>top7_author</th>\n",
       "      <th>top7_centrality</th>\n",
       "      <th>top8_author</th>\n",
       "      <th>top8_centrality</th>\n",
       "      <th>top9_author</th>\n",
       "      <th>top9_centrality</th>\n",
       "      <th>top10_author</th>\n",
       "      <th>top10_centrality</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>year</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2015</th>\n",
       "      <td>Ikki Fujiwara</td>\n",
       "      <td>0.103695</td>\n",
       "      <td>Stephan Olariu</td>\n",
       "      <td>0.103695</td>\n",
       "      <td>Chau Yuen</td>\n",
       "      <td>0.103695</td>\n",
       "      <td>D. Scott Wills</td>\n",
       "      <td>0.103695</td>\n",
       "      <td>Li Lu 0001</td>\n",
       "      <td>0.103695</td>\n",
       "      <td>Haibing Guan</td>\n",
       "      <td>0.103695</td>\n",
       "      <td>Jinquan Dai</td>\n",
       "      <td>0.103695</td>\n",
       "      <td>Chunbai Yang</td>\n",
       "      <td>0.103695</td>\n",
       "      <td>Guangwen Yang</td>\n",
       "      <td>0.103695</td>\n",
       "      <td>Isabelle Puaut</td>\n",
       "      <td>0.103695</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015</th>\n",
       "      <td>Ikki Fujiwara</td>\n",
       "      <td>0.103695</td>\n",
       "      <td>Stephan Olariu</td>\n",
       "      <td>0.103695</td>\n",
       "      <td>Chau Yuen</td>\n",
       "      <td>0.103695</td>\n",
       "      <td>D. Scott Wills</td>\n",
       "      <td>0.103695</td>\n",
       "      <td>Li Lu 0001</td>\n",
       "      <td>0.103695</td>\n",
       "      <td>Haibing Guan</td>\n",
       "      <td>0.103695</td>\n",
       "      <td>Jinquan Dai</td>\n",
       "      <td>0.103695</td>\n",
       "      <td>Chunbai Yang</td>\n",
       "      <td>0.103695</td>\n",
       "      <td>Guangwen Yang</td>\n",
       "      <td>0.103695</td>\n",
       "      <td>Isabelle Puaut</td>\n",
       "      <td>0.103695</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016</th>\n",
       "      <td>Naren Ramakrishnan</td>\n",
       "      <td>0.093728</td>\n",
       "      <td>Zied Lachiri</td>\n",
       "      <td>0.093658</td>\n",
       "      <td>Ying-Cherng Lan</td>\n",
       "      <td>0.093658</td>\n",
       "      <td>Roberto Uribeetxeberria</td>\n",
       "      <td>0.093658</td>\n",
       "      <td>Mingwei Li</td>\n",
       "      <td>0.093658</td>\n",
       "      <td>Tom Schipper</td>\n",
       "      <td>0.093658</td>\n",
       "      <td>Cong Li</td>\n",
       "      <td>0.093658</td>\n",
       "      <td>Riad Toufouti</td>\n",
       "      <td>0.093658</td>\n",
       "      <td>Laurent Delosi&amp;egrave;res</td>\n",
       "      <td>0.093658</td>\n",
       "      <td>Salah Saad</td>\n",
       "      <td>0.093658</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017</th>\n",
       "      <td>Natalia Juristo Juzgado</td>\n",
       "      <td>0.050265</td>\n",
       "      <td>Na Wang</td>\n",
       "      <td>0.050255</td>\n",
       "      <td>Pietro Hiram Guzzi</td>\n",
       "      <td>0.050252</td>\n",
       "      <td>Bruno Cuconato</td>\n",
       "      <td>0.050252</td>\n",
       "      <td>Shivani Sheth</td>\n",
       "      <td>0.050252</td>\n",
       "      <td>Mayukh Nair</td>\n",
       "      <td>0.050252</td>\n",
       "      <td>Ahmed E. Hassan</td>\n",
       "      <td>0.050252</td>\n",
       "      <td>Sohrab P. Shah</td>\n",
       "      <td>0.050252</td>\n",
       "      <td>Vinayak Agarwal</td>\n",
       "      <td>0.050252</td>\n",
       "      <td>Jagath Gunatilake</td>\n",
       "      <td>0.050252</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018</th>\n",
       "      <td>Yu Zhang</td>\n",
       "      <td>0.056182</td>\n",
       "      <td>Jie Li</td>\n",
       "      <td>0.056043</td>\n",
       "      <td>Chao Wang</td>\n",
       "      <td>0.056043</td>\n",
       "      <td>Yang Li</td>\n",
       "      <td>0.056036</td>\n",
       "      <td>Mohamed Othman</td>\n",
       "      <td>0.056003</td>\n",
       "      <td>Zhen Guo</td>\n",
       "      <td>0.056001</td>\n",
       "      <td>Jing Zhang</td>\n",
       "      <td>0.055994</td>\n",
       "      <td>Ting Qu</td>\n",
       "      <td>0.055990</td>\n",
       "      <td>Xiang Li</td>\n",
       "      <td>0.055989</td>\n",
       "      <td>Marjan Kuchaki Rafsanjani</td>\n",
       "      <td>0.055987</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                  top1_author  top1_centrality     top2_author  \\\n",
       "year                                                             \n",
       "2015            Ikki Fujiwara         0.103695  Stephan Olariu   \n",
       "2015            Ikki Fujiwara         0.103695  Stephan Olariu   \n",
       "2016       Naren Ramakrishnan         0.093728    Zied Lachiri   \n",
       "2017  Natalia Juristo Juzgado         0.050265         Na Wang   \n",
       "2018                 Yu Zhang         0.056182          Jie Li   \n",
       "\n",
       "      top2_centrality         top3_author  top3_centrality  \\\n",
       "year                                                         \n",
       "2015         0.103695           Chau Yuen         0.103695   \n",
       "2015         0.103695           Chau Yuen         0.103695   \n",
       "2016         0.093658     Ying-Cherng Lan         0.093658   \n",
       "2017         0.050255  Pietro Hiram Guzzi         0.050252   \n",
       "2018         0.056043           Chao Wang         0.056043   \n",
       "\n",
       "                  top4_author  top4_centrality     top5_author  \\\n",
       "year                                                             \n",
       "2015           D. Scott Wills         0.103695      Li Lu 0001   \n",
       "2015           D. Scott Wills         0.103695      Li Lu 0001   \n",
       "2016  Roberto Uribeetxeberria         0.093658      Mingwei Li   \n",
       "2017           Bruno Cuconato         0.050252   Shivani Sheth   \n",
       "2018                  Yang Li         0.056036  Mohamed Othman   \n",
       "\n",
       "      top5_centrality   top6_author  top6_centrality      top7_author  \\\n",
       "year                                                                    \n",
       "2015         0.103695  Haibing Guan         0.103695      Jinquan Dai   \n",
       "2015         0.103695  Haibing Guan         0.103695      Jinquan Dai   \n",
       "2016         0.093658  Tom Schipper         0.093658          Cong Li   \n",
       "2017         0.050252   Mayukh Nair         0.050252  Ahmed E. Hassan   \n",
       "2018         0.056003      Zhen Guo         0.056001       Jing Zhang   \n",
       "\n",
       "      top7_centrality     top8_author  top8_centrality  \\\n",
       "year                                                     \n",
       "2015         0.103695    Chunbai Yang         0.103695   \n",
       "2015         0.103695    Chunbai Yang         0.103695   \n",
       "2016         0.093658   Riad Toufouti         0.093658   \n",
       "2017         0.050252  Sohrab P. Shah         0.050252   \n",
       "2018         0.055994         Ting Qu         0.055990   \n",
       "\n",
       "                    top9_author  top9_centrality               top10_author  \\\n",
       "year                                                                          \n",
       "2015              Guangwen Yang         0.103695             Isabelle Puaut   \n",
       "2015              Guangwen Yang         0.103695             Isabelle Puaut   \n",
       "2016  Laurent Delosi&egrave;res         0.093658                 Salah Saad   \n",
       "2017            Vinayak Agarwal         0.050252          Jagath Gunatilake   \n",
       "2018                   Xiang Li         0.055989  Marjan Kuchaki Rafsanjani   \n",
       "\n",
       "      top10_centrality  \n",
       "year                    \n",
       "2015          0.103695  \n",
       "2015          0.103695  \n",
       "2016          0.093658  \n",
       "2017          0.050252  \n",
       "2018          0.055987  "
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.to_csv(\"./data/dblp/corePeople.csv\", index= True, header= True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
